<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : "/role/datagrid"
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData)
			$('#search_key').val() ? params.NameLike = $('#search_key').val() : ''
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"mDataProp" : "Id","sTtile" : "ID", "bSortable": false}
			,{"mDataProp" : "Name","sTtile" : "ID", "bSortable": false}
			,{"mDataProp": "Seq"} 
			,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"15%",
				"render": function ( data, type, full, meta ) { 
					btns = '<div class="tpl-table-black-operation">';
					btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-success am-btn-xs" onclick="selectResource('+ full.Id +');return false;">分配</a>';
					btns += '&nbsp;<a  class="am-btn am-btn-secondary am-btn-xs" onclick="editFun('+ full.Id +');return false">编辑</a>';
					btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-danger am-btn-xs" onclick="delRole('+ full.Id +');return false;">删除</a></div>';
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		  
	});
	
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
	});
	
	function delRole(id) {
		layer.confirm('确定要删除所选记录吗？删除后不可恢复！', function(index){
			$.post('{{ urlfor "RoleController.Delete"}}', {ids:id}, function(res){
				if(res.code == 1) {
					layer.msg(res.msg)
				} else {
					tables.ajax.reload();
				}
				layer.close(index);
			}, 'json');
		});
	}
	function dialogReback() {
		tables.ajax.reload();
	}
	
	function editFun(Id) {
        var title = '添加角色';
        if (Id > 0) {
            title = "编辑角色"
        }
        var url = '{{ urlfor "RoleController.Edit"}}' + '/' + Id;
        layer.open({
            type: 2,
            title: title,
            shadeClose: false,
            shade: 0.2,
            maxmin: true,
            shift: 1,
            area: ['600px', '270px'],
            content: url
        });
    }
	
	function selectResource(Id) {
        var url = '{{ urlfor "ResourceController.Select"}}' + '?desttype=1&destval=' + Id;
        var urlAllocate = '{{ urlfor "RoleController.Allocate"}}';
        layer.open({
            type: 2,
            title: '选择资源',
            shadeClose: false,
            shade: 0.2,
            maxmin: true,
            shift: 1,
            area: ['800px', '80%'],
            content: url,
			btn: ['保存', '关闭'],
            yes: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                var selectedids = iframeWin.getSelections();
				
                if (selectedids.length > 0) {
                    var datas={id:Id,ids:selectedids.join(',')};
                    $.post(urlAllocate,datas,function(re){
                        if (re.code === 0) {
                            layer.msg(re.msg); 
                            layer.close(index); 
                        }
                        else{
                            layer.alert(re.msg,{icon:2,title:'错误'});
                        }
                    });
                } else {
                    layer.alert("请选择资源",{icon:0,title:'提示'});
                }

            }
        });
    }
</script>
